//
//  NewsDetailViewController.swift
//  HeadlineNews
//
//  Created by han on 2019/7/5.
//  Copyright © 2019 han. All rights reserved.
//

import UIKit

class NewsDetailViewController: BaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "newsDetail"
        view.backgroundColor = UIColor.cyan
        self.hideNavigationBarIfNeeded = false
        self.popGestureEnable = true
    
        let button = UIButton(type: .system)
        button.setTitle("newsDetailButton", for: .normal)
        button.frame = CGRect(x: 50, y: 100, width: 200, height: 80)
        view.addSubview(button)
        button.addTarget(self, action: #selector(didClick), for: .touchUpInside)
    }
    
    @objc func didClick() {
        let detail = NewsDetailViewController()
        self.navigationController?.pushViewController(detail, animated: true)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
    }
    
    // 需要手势或者点击按钮返回时，跳过几个VC。
    override func updateStack() {
        guard let children = self.navigationController?.viewControllers, case var childrenVCs as [UIViewController] = NSMutableArray(array: children)  else { return }
        
        let count = childrenVCs.count
        
        guard  count > 2 else { return }
        // 判断上一个入栈的是否是NewsDetailViewController类型
        guard let _: NewsListViewController = childrenVCs[count - 2] as? NewsListViewController  else { return  }
        childrenVCs.remove(at: count - 2)
        //        // 删除上一条入栈
        //        last.willMove(toParent: nil)
        //        last.view.removeFromSuperview()
        //        last.removeFromParent()
        // 这样做会导致导航栏被移除的VC前一个VC继承。
        self.navigationController?.viewControllers = childrenVCs
        
    }
}


